package com.cjy.controller;

import com.cjy.pojo.Tag;
import com.cjy.pojo.User;
import com.cjy.service.TagService;
import com.cjy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public String user(Model model){

        model.addAttribute("user",new User());
        return "admin/user-input";
    }

    @GetMapping("/userList")
    public String userList(@PageableDefault(size = 8,sort = {"id"},direction = Sort.Direction.DESC)
                          Pageable pageable,Model model){
        Page<User> users = userService.listUser(pageable);
        model.addAttribute("page", users);
        return "admin/tagManager";
    }

    @PostMapping("/addUser")
    public String addUser(@Valid User user , BindingResult result, RedirectAttributes attributes){
//        Tag tagByName = tagService.getTagByName(user.getName());
//        if (tagByName!=null){
//            result.rejectValue("name","nameError","该标签已存在，不能重复添加！");
//        }
        // 后端验证数据是否为空
        if (result.hasErrors()){
            return "admin/user-input";
        }
        User t = userService.userSave(user);
        if (t==null){
            // 保存失败
            attributes.addFlashAttribute("message","新增失败");
        }else {
            attributes.addFlashAttribute("message","新增成功");
        }
        return "redirect:/admin/userList";
    }

    @PostMapping("/user/{id}")
    public String editType(@Valid User tag ,BindingResult result, @PathVariable Long id ,  RedirectAttributes attributes){
//        Tag tagByName = userService.getTagByName(tag.getName());
//        if (tagByName!=null){
//            // 给属性校验
//            result.rejectValue("name","nameError","该分类已存在，不能重复添加！");
//        }
        // 后端验证数据是否为空
        if (result.hasErrors()){
            return "admin/tag-input";
        }
        User t = userService.updateUser(id,tag);
        if (t==null){
            // 保存失败
            attributes.addFlashAttribute("message","更新失败");
        }else {
            attributes.addFlashAttribute("message","更新成功");
        }
        return "redirect:/admin/userList";
    }

    @GetMapping("/user/{id}/input")
    public String input(@PathVariable Long id, Model model){
        model.addAttribute("tag", userService.getUser(id));
        return "admin/user-input";
    }

    @GetMapping("/user/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        userService.deleteUser(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/userList";
    }


}
